﻿using DevExpress.XtraGrid.Demos.util;
using Luna小店.DTO;
using Luna小店.service;
using Luna小店.UC.款式异常;
using Luna小店.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Luna小店.UC.门店下单.form.Frm订单一览;

namespace Luna小店.UC.销售记录.service
{
    public class 销售记录Service
    {
        public static List<DTO销售表头> get店铺()
        {
            return
                FunctionHelper.JsonDeserialization<List<DTO销售表头>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/store/getSalesStore"
                        )
                    );
        }
        public static List<DTO销售表头> get销售员()
        {
            return
                FunctionHelper.JsonDeserialization<List<DTO销售表头>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/store/getSalesAssistant"
                        )
                    );
        }
        public static List<DTO销售表头> get支付方式()
        {
            return
                FunctionHelper.JsonDeserialization<List<DTO销售表头>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/store/getSalesPaymentmethod"
                        )
                    );
        }
        public static void post销售记录(DTO销售记录 DTO销售记录, Enum订单类别 @enum)
        {
            DTO销售记录.buildOrderDate();
            switch (@enum)
            {
                case Enum订单类别.销售订单:
                    RestCall.PostHttp(
                    URL.接口url + "/barcode/saveOrder_SSHIRT",
                    FunctionHelper.JsonSerialization(DTO销售记录)
                            );
                    break;
                case Enum订单类别.套号订单:
                    RestCall.PostHttp(
                    URL.接口url + "/barcode/saveOrder_SSHIRTTAG",
                    FunctionHelper.JsonSerialization(DTO销售记录)
                            );
                    break;
                case Enum订单类别.样品订单:
                    RestCall.PostHttp(
                    URL.接口url + "/barcode/saveOrder_SSHIRTSAMPLE",
                    FunctionHelper.JsonSerialization(DTO销售记录)
                            );
                    break;
                case Enum订单类别.Luna店铺订单:
                    RestCall.PostHttp(
                    URL.接口url + "/barcode/saveOrder_MRZHAO",
                    FunctionHelper.JsonSerialization(DTO销售记录)
                            );
                    break;
            }
        }
        public static void post备注图片(OrderDto orderDto)
        {
            foreach (备注图片 rp in orderDto.remarksPicture)
            {
                FileService.UploadRequest(
                    URL.接口url + "/image/uploadMemoImageByOrderCommitId?orderCommitId=" + orderDto.id,
                    rp.PATH,
                    rp.NAME,
                    "multipartFile");
            }
        }

    }

    public class OrderCommitFacade
    {
        public OrderCommitDto orderCommitDto { get; set; }
        public SalesConfirmationEntryDTO salesConfirmationEntryDTO { get; set; }
        public OrderCommitFacade()
        {

        }
        public OrderCommitFacade(OrderCommitDto orderCommitDto, SalesConfirmationEntryDTO salesConfirmationEntryDTO)
        {
            this.orderCommitDto = orderCommitDto;
            this.salesConfirmationEntryDTO = salesConfirmationEntryDTO;
        }
        public OrderCommitFacade buildOrderDate()
        {
            this.orderCommitDto.orderDate = DateTime.Now;
            return this;
        }
    }
    public class DTO销售记录
    {
        public String id { get; set; } //表头id
        public String storeId { get; set; }//门店id
        public String assistantId { get; set; }//销售员id
        public String discount { get; set; }//销售折让
        public double saleprice { get; set; }//应付款
        public double payment { get; set; }//实付
        public String paymentMethodId { get; set; }//支付方式id
        public String guest { get; set; }//客户名称
        public String salesSourceId { get; set; }//销售来源id
        public String createDate { get; set; }//创建日期
        public List<OrderCommitFacade> orderCommitFacades { get; set; } = new List<OrderCommitFacade>();//分录 
        public DTO销售记录()
        {

        }

        public DTO销售记录(
            String guest,
            String createDate,
            string storeId,
            string assistantId,
            string paymentMethodId,
            List<OrderCommitFacade> orderCommitFacades
            )
        {
            this.id = new FunctionHelper().Uuid;
            this.guest = guest;
            this.createDate = createDate;
            this.storeId = storeId;
            this.assistantId = assistantId;
            this.paymentMethodId = paymentMethodId;
            this.orderCommitFacades = orderCommitFacades;
        }
        /// <summary>
        /// 下单时间
        /// </summary>
        /// <returns></returns>
        public DTO销售记录 buildOrderDate()
        {
            foreach (OrderCommitFacade orderCommitFacade in this.orderCommitFacades)
            {
                orderCommitFacade.buildOrderDate();
            }
            return this;
        }

        public DTO销售记录 buildForKashiyamaUS(List<OrderDto> orderDtos)
        {
            this.id = new FunctionHelper().Uuid;
            foreach (OrderDto orderDto in orderDtos)
            {
                this.orderCommitFacades.Add(
                    new OrderCommitFacade(
                        new OrderCommitDto(orderDto),
                        new SalesConfirmationEntryDTO(orderDto,this.id)));
            }
            this.buildOrderDate();
            this.createDate = DateTime.Now.ToString();
            return this;
        }
    }
    public class DTO销售表头
    {
        public String id { get; set; }
        public String name { get; set; }
        public DTO销售表头()
        {

        }
        public DTO销售表头(String id, String name)
        {
            this.id = id;
            this.name = name;
        }
    }
    public class SalesConfirmationEntryDTO //DTO销售明细
    {
        public String id { get; set; }//分录id
        public String titleId { get; set; }//表头id
        public String salesTypeId { get; set; }//销售类型id
        public String styleId { get; set; }//款式Id
        public String materialNo { get; set; }//面料号
        public String size { get; set; }//尺码 
        public int quantity { get; set; }//数量
        public double price { get; set; }//售价
        public double totalPrice { get; set; }//总金额
        public SalesConfirmationEntryDTO(OrderDto OrderDto, String titleId)
        {
            this.titleId = titleId;
            this.id = new FunctionHelper().Uuid;
            //this.salesTypeId = OrderDto.orderType.ToString();
            this.styleId = OrderDto.refStyleId;
            this.materialNo = OrderDto.materialCode;
            this.size = OrderDto.styleSizeCd.Substring(15);
            this.quantity = Convert.ToInt32(OrderDto.orderNumber);
        }
        public SalesConfirmationEntryDTO(OrderCommitDto OrderDto)
        {
            this.id = new FunctionHelper().Uuid;
            //this.salesTypeId = OrderDto.orderType.ToString();
            this.styleId = OrderDto.refStyleId;
            this.materialNo = OrderDto.materialCode;
            this.size = OrderDto.styleSizeCd.Substring(15);
            this.quantity = Convert.ToInt32(OrderDto.orderNumber);
        }
    }

    /// <summary>
    /// 筛选出orderDto中有用的字段
    /// </summary>
    public class OrderCommitDto
    {
        //    public Enum订单类型 orderSalesType {get;set;}//订单类型
        public String id { get; set; }
        public String customerId { get; set; }
        public String customerName { get; set; }
        public String addressId { get; set; }
        public String customerCountId { get; set; }
        public String shopId { get; set; }
        public String shopName { get; set; }
        public String styleId { get; set; }
        public String orderNo { get; set; }
        public Double orderNumber { get; set; }
        public DateTime orderDate { get; set; }
        public String styleSizeCd { get; set; }
        public String styleNameCn { get; set; }
        public String styleYear { get; set; }
        public String styleSeason { get; set; }
        public String refStyleId { get; set; }
        public String styleFabricId { get; set; }
        public String materialNameCn { get; set; }
        public String materialCode { get; set; }  //面料号
        public String materialColor { get; set; }
        public String stylePublishCategoryCd { get; set; }
        public String styleCategoryCd { get; set; }
        public String styleFitCd { get; set; }
        public String styleSizeGroupCd { get; set; }
        public String styleDressCategory { get; set; }
        public String styleDesignType { get; set; }
        public String orderType { get; set; }
        public String remarks { get; set; }//备注
        public String styleMakeType { get; set; }//制作工艺
        public StyleAccessoryItemsDTO styleAccessoryItemsDTO { get; set; }//辅料
        public String saveStyleAccessoryItemsDTO { get; set; }//Json辅料保存
        public List<MeasureItemDTO> measureItemDTOS { get; set; } = new List<MeasureItemDTO>();//尺寸
        public String sessionId { get; set; }
        public String kashiyamaOrderNo { get; set; }
        public String fromId { get; set; }  //样品下单FormId

        public List<DTODesignPoints> dtoDesignPoints { get; set; } //设计点
        public DtoSize dtoSize { get; set; } //尺寸
        public String orderId { get; set; }

        public OrderCommitDto()
        {

        }
        /// <summary>
        /// 筛选orderDto中的字段转成新dto
        /// </summary>
        /// <param name="orderDto"></param>
        public OrderCommitDto(OrderDto orderDto)
        {
            this.id = orderDto.id;
            this.customerId = orderDto.customerId;
            this.customerName = orderDto.customerName;
            this.addressId = orderDto.addressId;
            this.customerCountId = orderDto.customerCountId;
            this.shopId = orderDto.shopId;
            this.shopName = orderDto.shopName;
            this.styleId = orderDto.styleId;
            this.orderNo = orderDto.orderNo;
            this.orderNumber = orderDto.orderNumber;
            this.orderDate = orderDto.orderDate;
            this.styleSizeCd = orderDto.styleSizeCd;
            this.styleNameCn = orderDto.styleNameCn;
            this.styleYear = orderDto.styleYear;
            this.styleSeason = orderDto.styleSeason;
            this.refStyleId = orderDto.refStyleId;
            this.styleFabricId = orderDto.styleFabricId;
            this.materialNameCn = orderDto.materialNameCn;
            this.materialCode = orderDto.materialCode;
            this.materialColor = orderDto.materialColor;
            this.stylePublishCategoryCd = orderDto.stylePublishCategoryCd;
            this.styleCategoryCd = orderDto.styleCategoryCd;
            this.styleFitCd = orderDto.styleFitCd;
            this.styleSizeGroupCd = orderDto.styleSizeGroupCd;
            this.styleDressCategory = orderDto.styleDressCategory;
            this.styleDesignType = orderDto.styleDesignType;
            this.orderType = orderDto.orderType.ToString();
            this.remarks = orderDto.remarks;
            this.styleMakeType = orderDto.styleMakeType;
            this.styleAccessoryItemsDTO = orderDto.styleAccessoryItemsDTO;
            this.saveStyleAccessoryItemsDTO = orderDto.saveStyleAccessoryItemsDTO;
            this.sessionId = orderDto.sessionId;
            this.kashiyamaOrderNo = orderDto.kashiyamaOrderNo;
            this.fromId = orderDto.fromId;
            this.dtoDesignPoints = orderDto.dtoDesignPoints;
            this.dtoSize = orderDto.dtoSize;
            this.measureItemDTOS = orderDto.measureItemDTOS;
        }

    }
}
